package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.OperationlogService;
import com.youi.entity.Operationlog;
import com.youi.entity.Sysuser;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 操作日志控制层
* @author Administrator
*/
@Controller
public class OperationlogController  extends HttpRequestManage{

		@Autowired
		private OperationlogService operationlogService;

		/**
		 *添加或编辑操作日志
		*/
		@ResponseBody
		@RequestMapping("addOrEditOperationlog")
		public String addOrEditOperationlog(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Operationlog operationlog=null;
			if (id!=null) {
				operationlog=operationlogService.get(id);
			}else {
				operationlog=new Operationlog();
				operationlog.setDelStatus(DelStatus.NODEL);
				operationlog.setGmtCreated(new Date());
			}
			Long sysuserId=optLong("sysuserId",request);
			if (sysuserId!=null){
				operationlog.setSysuserId(sysuserId);
			}
			String ip=optString("ip",request);
			if (ip!=null){
				operationlog.setIp(ip);
			}
			String address=optString("address",request);
			if (address!=null){
				operationlog.setAddress(address);
			}
			String action=optString("action",request);
			if (action!=null){
				operationlog.setAction(action);
			}
			operationlog.setGmtModify(new Date());
			operationlogService.saveOrUpdate(operationlog);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除操作日志
		*/
		@ResponseBody
		@RequestMapping("delOperationlogByIds")
		public String delOperationlogByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Operationlog> operationlogList= operationlogService.getByParams(params);
			if (operationlogList==null||operationlogList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Operationlog operationlog : operationlogList) {
				operationlog.setDelStatus(DelStatus.DEL);
			}
			operationlogService.saveOrUpdateAll(operationlogList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-5
		 * 查询操作日志列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getOperationlog")
		public String getOperationlog(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Operationlog> operationlogList=operationlogService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (operationlogList==null||operationlogList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=operationlogService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, operationlogList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询操作日志
		*/
		@ResponseBody
		@RequestMapping("getOperationlogById")
		public String getOperationlogById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Operationlog operationlog=operationlogService.get(id);
			if (operationlog==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",operationlog);//jsonObject	json对象
		}

		/**
		 * 查询所有操作日志
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllOperationlog")
		public String getAllOperationlog(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Operationlog> operationlogList= operationlogService.getByParams(null);
			if (operationlogList==null||operationlogList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",operationlogList);//jsonObjectList	json对象集合
		}
}
